package shplate

import java.text.SimpleDateFormat;

/**
 * @Program: SHPLATE
 * @Description: 获取时间工具类
 */
class DateUtil {

    /**
     * 获取开始时间
     * @param vStartTime
     * @return
     */
    static def getStartTime(vStartTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(Constant.DEFAULT_DATA_PATTERN)
        def startTime = vStartTime ? sdf.parse(vStartTime) : sdf.parse(sdf.format(new Date().clearTime()))
        startTime = sdf.format(startTime)
        [
                startTime: startTime
        ]
    }

    /**
     * 获取结束时间
     * @param vEndTime
     * @return
     */
    static def getEndTime(vEndTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(Constant.DEFAULT_DATA_PATTERN)
        def endTime = vEndTime ? sdf.parse(vEndTime) : sdf.parse(sdf.format(new Date()))
        endTime = sdf.format(endTime)
        [
                endTime: endTime
        ]
    }

    /**
     * 获取开始日期
     * @param vStartDate
     * @return
     */
    static def getStartDate(vStartDate) {
        SimpleDateFormat sdf = new SimpleDateFormat(Constant.DAY_DATA_PATTERN)
        def startDate = vStartDate ? sdf.parse(vStartDate) : sdf.parse(sdf.format(new Date().clearTime()))
        startDate = sdf.format(startDate)
        [
                startDate: startDate
        ]
    }

    /**
     * 获取结束日期
     * @param vEndDate
     * @return
     */
    static def getEndDate(vEndDate) {
        SimpleDateFormat sdf = new SimpleDateFormat(Constant.DAY_DATA_PATTERN)
        def endDate = vEndDate ? sdf.parse(vEndDate) : sdf.parse(sdf.format(new Date().clearTime()))
        endDate = sdf.format(endDate)
        [
                endDate: endDate
        ]
    }

    /**
     * 获取当年
     * @param vYearTime
     * @return
     */
    static def getYearTime(vYearTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(Constant.YEAR_DATA_PATTERN)
        def yearTime = vYearTime ? sdf.parse(vYearTime) : sdf.parse(sdf.format(new Date()))
        yearTime = sdf.format(yearTime)
        [
                yearTime: yearTime
        ]
    }

    /**
     * 获取开始当月
     * @param vMonthTime
     * @return
     */
    static def getStartMonthTime(vMonthTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(Constant.MONTH_DATA_PATTERN)
        def startMonthTime = vMonthTime ? sdf.parse(vMonthTime) : sdf.parse(sdf.format(new Date()))
        startMonthTime = sdf.format(startMonthTime)
        [
                startMonthTime: startMonthTime
        ]
    }

    /**
     * 获取结束当月
     * @param vMonthTime
     * @return
     */
    static def getEndMonthTime(vMonthTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(Constant.MONTH_DATA_PATTERN)
        def endMonthTime = vMonthTime ? sdf.parse(vMonthTime) : sdf.parse(sdf.format(new Date()))
        endMonthTime = sdf.format(endMonthTime)
        [
                endMonthTime: endMonthTime
        ]
    }

    /**
     * 获取两个日期之间的日期
     * @Author Ivan
     * @Date 2019/3/22
     * @Params
     * @Return
     */
    static List<String> getBetweenDates(Date startTime, Date endTime) {
        List<String> result = new ArrayList<String>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(startTime);
        tempStart.add(Calendar.DAY_OF_YEAR, 1);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(endTime);
        SimpleDateFormat sdf;
        while (tempStart.before(tempEnd)) {
            sdf = new SimpleDateFormat("yyyy-MM-dd");
            result.add(sdf.format(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }
}